package team.seekers.hula.course.resource.sectionContentComment.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.enums.WindTypeEnum;
import team.seekers.hula.common.utils.BaseConvertUtils;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.course.resource.sectionContent.service.SectionContentService;
import team.seekers.hula.course.resource.sectionContentComment.entity.dao.SectionContentCommentDO;
import team.seekers.hula.course.resource.sectionContentComment.entity.dto.SectionContentCommentAddDTO;
import team.seekers.hula.course.resource.sectionContentComment.entity.dto.SectionContentCommentQueryDTO;
import team.seekers.hula.course.resource.sectionContentComment.entity.dto.SectionContentCommentUpdateDTO;
import team.seekers.hula.course.resource.sectionContentComment.entity.vo.SectionContentCommentListVO;
import team.seekers.hula.course.resource.sectionContentComment.mapper.SectionContentCommentMapper;
import team.seekers.hula.message.message.service.MessageService;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.service.UserService;
import team.seekers.hula.record.wind.service.WindService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hbc
 * @since 2021-02-18
 */
@Service
public class SectionContentCommentService extends ServiceImpl<SectionContentCommentMapper, SectionContentCommentDO> {

    @Autowired
    @Lazy
    private SectionContentService sectionContentService;

    @Autowired
    @Lazy
    private UserService userService;

    @Autowired
    @Lazy
    private MessageService messageService;

    @Autowired
    private WindService windService;


    public SectionContentCommentListVO get(String id){
        SectionContentCommentListVO commentListVO = new SectionContentCommentListVO();
        SectionContentCommentDO contentCommentDO = getById(id);
        BeanUtils.copyProperties(contentCommentDO,commentListVO);
        commentListVO.setUserInfo(userService.getFollowInfoVO(contentCommentDO.getUserId(),MindUtils.getUserId()));
        if(!contentCommentDO.getUserId().equals(MindUtils.getUserId())){
            commentListVO.setIsMe(true);
        }
        commentListVO.setPictures(BaseConvertUtils.stringToList(contentCommentDO.getPictures()));
        return commentListVO;
    }


    public PageResult<SectionContentCommentListVO> pageOrderByGood(SectionContentCommentQueryDTO queryDTO) {
        LambdaQueryChainWrapper<SectionContentCommentDO> lambdaQuery = lambdaQuery();

        if(!StringUtils.isEmpty(queryDTO.getBegin())){
            lambdaQuery.ge(SectionContentCommentDO::getGmtCreate,queryDTO.getBegin());
        }
        if(!StringUtils.isEmpty(queryDTO.getEnd())){
            lambdaQuery.le(SectionContentCommentDO::getGmtCreate,queryDTO.getEnd());
        }

        lambdaQuery.orderByDesc(SectionContentCommentDO::getWind);

        Page<SectionContentCommentDO> page = lambdaQuery.page(new Page<>(queryDTO.getPageNow(), queryDTO.getPageSize()));
        long total = page.getTotal();
        List<SectionContentCommentDO> records = page.getRecords();
        if(records == null){
            return null;
        }
        List<SectionContentCommentListVO> list = new ArrayList<>();
        for (SectionContentCommentDO record : records) {
            SectionContentCommentListVO answerCommentListVO = new SectionContentCommentListVO();
            BeanUtils.copyProperties(record,answerCommentListVO);
            UserDO userDO = userService.getById(record.getUserId());
            if(userDO == null){
                return null;
            }
            if(!record.getUserId().equals(MindUtils.getUserId())){
                answerCommentListVO.setIsMe(true);
            }
            answerCommentListVO.setPictures(BaseConvertUtils.stringToList(record.getPictures()));
            answerCommentListVO.setUserInfo(userService.getFollowInfoVO(record.getUserId(),MindUtils.getUserId()));
            list.add(answerCommentListVO);
        }
        return new PageResult<>(total,list);
    }

    public PageResult<SectionContentCommentListVO> pageOrderByTime(SectionContentCommentQueryDTO queryDTO) {
        LambdaQueryChainWrapper<SectionContentCommentDO> lambdaQuery = lambdaQuery().orderByDesc(SectionContentCommentDO::getGmtCreate);

        if(!StringUtils.isEmpty(queryDTO.getBegin())){
            lambdaQuery.ge(SectionContentCommentDO::getGmtCreate,queryDTO.getBegin());
        }
        if(!StringUtils.isEmpty(queryDTO.getEnd())){
            lambdaQuery.le(SectionContentCommentDO::getGmtCreate,queryDTO.getEnd());
        }

        if(!StringUtils.isEmpty(queryDTO.getNickname())){
            List<String> userIds =  userService.listIdsLikeNickname(queryDTO.getNickname());
            if(userIds != null && userIds.size() > 0){
                lambdaQuery.in(SectionContentCommentDO::getUserId,userIds);
            }else {
                return new PageResult<>();
            }
        }

        if(!StringUtils.isEmpty(queryDTO.getContent())){
            lambdaQuery.like(SectionContentCommentDO::getContent,queryDTO.getContent());
        }

        if(!StringUtils.isEmpty(queryDTO.getUserId())){
            lambdaQuery.eq(SectionContentCommentDO::getUserId,queryDTO.getUserId());
        }

        if(!StringUtils.isEmpty(queryDTO.getCourseId())){
            lambdaQuery.eq(SectionContentCommentDO::getCourseId,queryDTO.getCourseId());
        }

        if(!StringUtils.isEmpty(queryDTO.getSectionContentId())){
            lambdaQuery.eq(SectionContentCommentDO::getSectionContentId,queryDTO.getSectionContentId());
        }

        if(!StringUtils.isEmpty(queryDTO.getSectionId())){
            ArrayList<String> sectionContentIdList = sectionContentService.getBindContentIdListBySectionId(queryDTO.getSectionId());
            if(sectionContentIdList != null && sectionContentIdList.size() > 0)
                lambdaQuery.in(SectionContentCommentDO::getSectionContentId,sectionContentIdList);
        }

        Page<SectionContentCommentDO> page = lambdaQuery.page(new Page<>(queryDTO.getPageNow(), queryDTO.getPageSize()));
        long total = page.getTotal();
        List<SectionContentCommentDO> records = page.getRecords();
        if(total == 0){
            return new PageResult<>();
        }
        List<SectionContentCommentListVO> list = new ArrayList<>();
        for (SectionContentCommentDO record : records) {
            SectionContentCommentListVO answerCommentListVO = new SectionContentCommentListVO();
            BeanUtils.copyProperties(record,answerCommentListVO);
            if(!record.getUserId().equals(MindUtils.getUserId())){
                answerCommentListVO.setIsMe(true);
            }
            answerCommentListVO.setPictures(BaseConvertUtils.stringToList(record.getPictures()));
            answerCommentListVO.setUserInfo(userService.getFollowInfoVO(record.getUserId(),MindUtils.getUserId()));
            list.add(answerCommentListVO);
        }
        return new PageResult<>(total,list);
    }


    public String add(SectionContentCommentAddDTO addDTO) {

        // 新增评论
        SectionContentCommentDO commentDO = new SectionContentCommentDO();
        BeanUtils.copyProperties(addDTO, commentDO);
        commentDO.setPictures(BaseConvertUtils.listToString(addDTO.getPictures()));

        if(addDTO.getIsQuestion()){
            //TODO 给主理人发送信息
            commentDO.setIsQuestion(1);
        }

        commentDO.setUserId(MindUtils.getUserId());
        commentDO.setCourseId(sectionContentService.getCourseIdById(addDTO.getSectionContentId()));
        save(commentDO);

        // 新增wind
        windService.addWind(WindTypeEnum.ADD_COMMENT,commentDO.getId(),commentDO.getVisibility());


        return commentDO.getId();
    }

    public void delete(String id) {
        // 判断是不是该用户的评论
        SectionContentCommentDO commentDO = getById(id);
        if(!MindUtils.isAdmin()){
            if(!commentDO.getUserId().equals(MindUtils.getUserId())){
                return;
            }
        }

        // 删除评论
        removeById(id);

        // 删除wind
        windService.removeWind(WindTypeEnum.ADD_COMMENT,id);
        windService.removeWind(WindTypeEnum.ADD_REPLAY,id);
    }

    public void replay(SectionContentCommentUpdateDTO updateDTO) {
        SectionContentCommentDO answerCommentDO = getById(updateDTO.getId());
        //更新评论
        answerCommentDO.setReply(updateDTO.getReply());
        updateById(answerCommentDO);
        windService.addWind(WindTypeEnum.ADD_REPLAY,answerCommentDO.getId(),2);
    }

    public void update(SectionContentCommentUpdateDTO updateDTO) {
        // 判断是不是该用户的评论
        SectionContentCommentDO answerCommentDO = getById(updateDTO.getId());
        if(!MindUtils.isAdmin()){
            if(!answerCommentDO.getUserId().equals(MindUtils.getUserId())){
                return;
            }
        }
        //更新评论
        BeanUtils.copyProperties(updateDTO, answerCommentDO);
        updateById(answerCommentDO);
    }

    public void deleteBySectionContentId(String sectionContentId) {
        lambdaUpdate().eq(SectionContentCommentDO::getSectionContentId,sectionContentId).remove();
    }

    public void addWind(String sectionContentCommentId) {
        Integer wind = lambdaQuery().select(SectionContentCommentDO::getWind).eq(SectionContentCommentDO::getId, sectionContentCommentId).one().getWind();
        lambdaUpdate().set(SectionContentCommentDO::getWind,wind+1).eq(SectionContentCommentDO::getId, sectionContentCommentId).update();
    }

    public List<SectionContentCommentListVO> listOrderByTime(String sectionContentId) {
        List<SectionContentCommentDO> sectionContentCommentDOS = lambdaQuery()
                .eq(SectionContentCommentDO::getSectionContentId, sectionContentId)
                .orderByDesc(SectionContentCommentDO::getTime)
                .list();

        if(sectionContentCommentDOS.size() == 0){
            return new ArrayList<>();
        }

        List<SectionContentCommentListVO> list = new ArrayList<>();

        for (SectionContentCommentDO record : sectionContentCommentDOS) {
            SectionContentCommentListVO answerCommentListVO = new SectionContentCommentListVO();
            BeanUtils.copyProperties(record,answerCommentListVO);
            if(record.getUserId().equals(MindUtils.getUserId())){
                answerCommentListVO.setIsMe(true);
            }else{
                answerCommentListVO.setIsMe(false);
            }
            answerCommentListVO.setPictures(BaseConvertUtils.stringToList(record.getPictures()));
            answerCommentListVO.setUserInfo(userService.getFollowInfoVO(record.getUserId(),MindUtils.getUserId()));
            list.add(answerCommentListVO);
        }

        return list;
    }
}
